package com.top.ruizhong.main.access

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.module.lemlin.mode.ViewModeFragment
import com.top.ruizhong.R
import com.top.ruizhong.mode.vm.AccessViewModel
import kotlinx.android.synthetic.main.fragment_access_code.*

class CodeFragment : ViewModeFragment<AccessViewModel>() {

    companion object {

        private val ARRAY_CODE: Array<String?> = arrayOfNulls(6)

        private const val EXTRA_NAME_MOBILE = "EXTRA_NAME_MOBILE"
        fun newInstance(mobile: String): CodeFragment {
            val bundle: Bundle = Bundle().apply {
                putSerializable(EXTRA_NAME_MOBILE, mobile)
            }
            return CodeFragment().apply { arguments = bundle }
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_code

    override fun viewModel(): AccessViewModel {
        return createViewModel(requireActivity())
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun initView() {

        arguments?.getString(EXTRA_NAME_MOBILE)?.let {
            tvCheckCodeMobile.text = it
        }

        with(etCheckCodeContent) {
            isLongClickable = false
            setTextIsSelectable(false)
            setOnClickListener {
                setSelection(text.length)
                isCursorVisible = false
            }
            setOnTouchListener { _, event ->
                if (event.action == MotionEvent.ACTION_UP) {
                    setSelection(text.length)
                    isCursorVisible = false
                }
                return@setOnTouchListener false
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        viewModel.clearLiveDataCheckCode()
    }

    override fun initViewMode() {
        viewModel.contentCheckCodeLiveData.observe(viewLifecycleOwner, Observer { code ->
            fillDataCode(code = code ?: "")
            if (code == null || code.length != 4)
                return@Observer
            arguments?.getString(EXTRA_NAME_MOBILE)?.let {
                viewModel.userLogin(it, code.toString())
            }
        })
    }

    override fun initData() {
        KeyboardUtils.showSoftInput(etCheckCodeContent)
        viewModel.observableEditText(etCheckCodeContent)
    }

    /**
     * 设置验证码数据
     * */
    private fun fillDataCode(code: CharSequence) {
        val length: Int = code.length
        for (i: Int in (if (length == 0) 0 else length - 1)
                until ARRAY_CODE.size) {
            ARRAY_CODE[i] = null
        }
        for (i: Int in 0 until length) {
            ARRAY_CODE[i] = code.toList()[i].toString()
        }
        if (length != 0) {
            ARRAY_CODE[length - 1] = code.last().toString()
        }
        tvCheckCode1.text = ARRAY_CODE[0]
        tvCheckCode2.text = ARRAY_CODE[1]
        tvCheckCode3.text = ARRAY_CODE[2]
        tvCheckCode4.text = ARRAY_CODE[3]
        tvCheckCode5.text = ARRAY_CODE[4]
        tvCheckCode6.text = ARRAY_CODE[5]

        if (code.length == 6) {
            KeyboardUtils.hideSoftInput(etCheckCodeContent)
        }
    }

}